#include "operator.h"

/** a void value **/
Value::Value() 
{
	type = NONE;
	isNumber = isBlock = isFunction = false;
}

/** number **/
Value::Value(double d) // number
{
	type = NUMBER;
	isNumber = true;
	isBlock = isFunction = false;
	number = d;
}

/** initialize a block of size s **/
Value::Value(void *p, int s) 
:block(p,s)
{
	type = BLOCK;
	isBlock = true;
	isNumber = isFunction = false;		
}

/** initialize a function **/
Value::Value(Function f) // function
{
	type = FUNCTION;
	isFunction = true;
	isNumber = isBlock = false;
	function = f;
}

/** force convert to number: 
 ** NOTE: default value is 0.0, which may affect the program s
 **/
double toNumber(Value val)
{
	if(val.isNumber)
		return val.number;
	else return 0.0;
}

Function toFunction(Value val)
{
	if(val.isFunction)
		return val.function;
	else /** return default on imcompatible val**/
		return doNothing;
}

Block toBlock(Value val)
{
	if(val.isBlock)
		return val.block;
	else
		return Block();
}

/** do nothing returns a void value **/
Value doNothing(Args a) { 
	return Value();
}


Value add(Args args) {
	return toNumber(args.get(0)) + toNumber(args.get(1)); // call Value(double)
}

Value subtract(Args args) {
	return toNumber(args.get(0)) - toNumber(args.get(1)); 
}



